package com.luoran.bartalks.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.entity.biz.BtCodingRule;
import com.luoran.bartalks.service.IBtCodingRuleService;
import com.luoran.bartalks.utils.EnumBillType;

/**
 * 用于初始化编码规则
 * 
 * @author 11057
 *
 */
@Controller
@RequestMapping("/test")
public class TestAction {

	@Autowired
	private IBtCodingRuleService codingRuleService;

	@RequestMapping
	public String test() {

		return "test";
	}

	@RequestMapping("/map")
	public String map() {

		return "map";
	}

	@RequestMapping("/sjksh")
	public String sjksh() {

		return "sjksh";
	}

	
	/**
	 * 编码规则初始化
	 */
	@RequestMapping("/initCodingRule")
	@ResponseBody
	public JSONObject initCodingRule() {
		JSONObject result = new JSONObject();
		String companyId = UserContext.get().getCompanyId();// 当前登录账号的公司ID.
		// 先清空该公司编码规则
		codingRuleService.deleteRulesByCompanyId(companyId);
		// 编码生成规则对象
		BtCodingRule codingRule1 = new BtCodingRule();
		BtCodingRule codingRule2 = new BtCodingRule();
		BtCodingRule codingRule3 = new BtCodingRule();
		BtCodingRule codingRule4 = new BtCodingRule();
		BtCodingRule codingRule5 = new BtCodingRule();
		BtCodingRule codingRule6 = new BtCodingRule();
		BtCodingRule codingRule7 = new BtCodingRule();
		BtCodingRule codingRule8 = new BtCodingRule();
		BtCodingRule codingRule9 = new BtCodingRule();

		codingRule1.setCompanyId(companyId);// 公司ID
		codingRule1.setSequenceLength(5);// 序列长度
		codingRule1.setSequenceBeginValue(0);// 起步值
		codingRule1.setStepLengthValue(1);// 步长值
		codingRule1.setCreateTime(new Date());// 创建日期
		codingRule1.setRuleStatus(0);// 状态
		codingRule1.setRuleDesc("初始化编码生成规则");

		codingRule2.setCompanyId(companyId);// 公司ID
		codingRule2.setSequenceLength(5);// 序列长度
		codingRule2.setSequenceBeginValue(0);// 起步值
		codingRule2.setStepLengthValue(1);// 步长值
		codingRule2.setCreateTime(new Date());// 创建日期
		codingRule2.setRuleStatus(0);// 状态
		codingRule2.setRuleDesc("初始化编码生成规则");

		codingRule3.setCompanyId(companyId);// 公司ID
		codingRule3.setSequenceLength(5);// 序列长度
		codingRule3.setSequenceBeginValue(0);// 起步值
		codingRule3.setStepLengthValue(1);// 步长值
		codingRule3.setCreateTime(new Date());// 创建日期
		codingRule3.setRuleStatus(0);// 状态
		codingRule3.setRuleDesc("初始化编码生成规则");

		codingRule4.setCompanyId(companyId);// 公司ID
		codingRule4.setSequenceLength(5);// 序列长度
		codingRule4.setSequenceBeginValue(0);// 起步值
		codingRule4.setStepLengthValue(1);// 步长值
		codingRule4.setCreateTime(new Date());// 创建日期
		codingRule4.setRuleStatus(0);// 状态
		codingRule4.setRuleDesc("初始化编码生成规则");

		codingRule5.setCompanyId(companyId);// 公司ID
		codingRule5.setSequenceLength(5);// 序列长度
		codingRule5.setSequenceBeginValue(0);// 起步值
		codingRule5.setStepLengthValue(1);// 步长值
		codingRule5.setCreateTime(new Date());// 创建日期
		codingRule5.setRuleStatus(0);// 状态
		codingRule5.setRuleDesc("初始化编码生成规则");

		codingRule6.setCompanyId(companyId);// 公司ID
		codingRule6.setSequenceLength(5);// 序列长度
		codingRule6.setSequenceBeginValue(0);// 起步值
		codingRule6.setStepLengthValue(1);// 步长值
		codingRule6.setCreateTime(new Date());// 创建日期
		codingRule6.setRuleStatus(0);// 状态
		codingRule6.setRuleDesc("初始化编码生成规则");

		codingRule7.setCompanyId(companyId);// 公司ID
		codingRule7.setSequenceLength(5);// 序列长度
		codingRule7.setSequenceBeginValue(0);// 起步值
		codingRule7.setStepLengthValue(1);// 步长值
		codingRule7.setCreateTime(new Date());// 创建日期
		codingRule7.setRuleStatus(0);// 状态
		codingRule7.setRuleDesc("初始化编码生成规则");

		codingRule8.setCompanyId(companyId);// 公司ID
		codingRule8.setSequenceLength(5);// 序列长度
		codingRule8.setSequenceBeginValue(0);// 起步值
		codingRule8.setStepLengthValue(1);// 步长值
		codingRule8.setCreateTime(new Date());// 创建日期
		codingRule8.setRuleStatus(0);// 状态
		codingRule8.setRuleDesc("初始化编码生成规则");

		codingRule9.setCompanyId(companyId);// 公司ID
		codingRule9.setSequenceLength(5);// 序列长度
		codingRule9.setSequenceBeginValue(0);// 起步值
		codingRule9.setStepLengthValue(1);// 步长值
		codingRule9.setCreateTime(new Date());// 创建日期
		codingRule9.setRuleStatus(0);// 状态
		codingRule9.setRuleDesc("初始化编码生成规则");

		codingRule1.setBillType(EnumBillType.AREA.getName());// 区域
		codingRule1.setCodingPrefix("QY-");// 区域编码前缀
		String id1 = codingRuleService.addCodingRule(codingRule1);

		codingRule2.setBillType(EnumBillType.POSITION.getName());// 位置
		codingRule2.setCodingPrefix("WZ-");// 位置编码前缀
		String id2 = codingRuleService.addCodingRule(codingRule2);

		codingRule3.setBillType(EnumBillType.CONTAINER.getName());// 容器
		codingRule3.setCodingPrefix("RQ-");// 容器编码前缀
		String id3 = codingRuleService.addCodingRule(codingRule3);

		codingRule4.setBillType(EnumBillType.CONTAINERTEMPLATE.getName());// 容器模板
		codingRule4.setCodingPrefix("RQMB-");// 容器模板编码前缀
		String id4 = codingRuleService.addCodingRule(codingRule4);

		codingRule5.setBillType(EnumBillType.TRANSPORTMETHOD.getName());// 运输方式
		codingRule5.setCodingPrefix("YSFS-");// 区域编码前缀
		String id5 = codingRuleService.addCodingRule(codingRule5);

		codingRule6.setBillType(EnumBillType.PLANTEMPLATE.getName());// 计划模板
		codingRule6.setCodingPrefix("JHMB-");// 计划模板编码前缀
		String id6 = codingRuleService.addCodingRule(codingRule6);

		codingRule7.setBillType(EnumBillType.PROJECT.getName());// 项目
		codingRule7.setCodingPrefix("XM-");// 项目编码前缀
		String id7 = codingRuleService.addCodingRule(codingRule7);

		codingRule8.setBillType(EnumBillType.PLAN.getName());// 计划
		codingRule8.setCodingPrefix("JH-");// 区域编码前缀
		String id8 = codingRuleService.addCodingRule(codingRule8);

		codingRule9.setBillType(EnumBillType.PACKINGLIST.getName());// 箱单
		codingRule9.setCodingPrefix("QY-");// 区域编码前缀
		String id9 = codingRuleService.addCodingRule(codingRule9);

		if (id1 != null && id2 != null && id3 != null && id4 != null && id5 != null && id6 != null && id7 != null
				&& id8 != null && id9 != null) {
			result.put("flag", true);
			result.put("msg", "编码规则初始化完成!");
			return result;
		} else {
			result.put("flag", false);
			result.put("msg", "编码规则初始化异常!");
			return result;
		}

	}

}
